package com.homework.action;

import java.io.Serializable;
import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.homework.bean.UserBean;
import com.homework.service.UserService;
import com.homework.util.Constants;

public class LoginAction implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 4649649617649240557L;
	private String username;
	private String password;
	
	private UserService userService;

	public LoginAction() {
		userService = new UserService();
	}

	// 登陆
	public String login() {
		//数据库的配置文件加载：
		//connDB.loadProperty();
		
		HttpServletRequest request = ServletActionContext.getRequest();
		
		UserBean userBean = new UserBean();
		userBean.setUsername(this.getUsername());
		userBean.setPassword(this.getPassword());
		try {
			
			int result = userService.login(userBean);
			//通过验证
			if (result == Constants.RESULT_STUDENT || result == Constants.RESULT_TEACHER || result == Constants.RESULT_ADMIN) {
				return "Success";
			} else if(result == Constants.RESULT_UsernameAndPwdWrong){// 用户名或密码输入错误
				request.setAttribute("message", Constants.MESSAGE_ERROR_UsernameAndPwdWrong);
				return "LoginError";
			} else if(result == Constants.RESULT_UserVerifyMissing){//该用户还未通过审核
				request.setAttribute("message",Constants.MESSAGE_ERROR_UserVerifyMissing);
				return "LoginError";
			} else if(result == Constants.RESULT_UserDeleted){//该用户已删除
				request.setAttribute("message",Constants.MESSAGE_ERROR_UserDeleted);
				return "LoginError";
			} else if(result == Constants.RESULT_SystemAbnormal){//系统异常
				request.setAttribute("message",Constants.MESSAGE_ERROR_SystemAbnormal);
				return "LoginError";
			} 
//			else if(result == Constants.RESULT_Online){//该用户已在线
//				request.setAttribute("message",Constants.MESSAGE_ERROR_Online);
//				return "LoginError";
//			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			request.setAttribute("message", Constants.MESSAGE_ERROR_SystemAbnormal);
			return "Error";// 系统异常
		}
		return "Error";
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}


}
